<?php

//require_once (SRC_DIR . '/framework/Model/Model.php');
require_once (SRC_DIR . '/framework/Model/Entity.php');



class TestEntity extends UnitTestCase	{
	
	public function testPrimaryKeySetting()	{
		
		$entity = new Entity();
		
		$primaryKey = array('col1', 'col2');
		
		$this->assertEqual($entity->getPrimaryKey(), array('id'));
		
		$entity->setPrimaryKey($primaryKey);
		
		$this->assertEqual($entity->getPrimaryKey(), $primaryKey);
	}

	public function testPrimaryKeyValues()	{
		$entity = new Entity();
		
		$entity->setPrimaryKey(array('id1', 'id2'));
		
		$this->assertFalse($entity->isPrimaryKeySet());
		
		$entity->id1 = 'wibble';
		$this->assertFalse($entity->isPrimaryKeySet());
		$entity->id2 = 'wobble';
		$this->assertTrue($entity->isPrimaryKeySet());
		$entity->another = 'dribble';
		$this->assertTrue($entity->isPrimaryKeySet());
		
		$this->assertEqual($entity->getPrimaryKeyValue(), array('id1' => 'wibble', 'id2' => 'wobble'));
	}
	
	public function testGetRelatedValues()	{
		$entity = new Entity();
		$this->assertEqual($entity->isRequestForRelatedEntities('getAll_foo', array()), 'foo');
		$this->assertEqual($entity->isRequestForRelatedEntities('getAll_foo_entity', array()), 'foo_entity');
		
		$this->assertFalse($entity->isRequestForRelatedEntities('getAll_foo_!entity', array()));
		$this->assertFalse($entity->isRequestForRelatedEntities('poo', array()));
		$this->assertFalse($entity->isRequestForRelatedEntities('poo_getAll_foo', array()));
	}
	
	public function testGetRelatedEntity()	{
		$entity = new Entity();
		$this->assertEqual($entity->isRequestForRelatedEntity('get_foo', array()), 'foo');
		$this->assertEqual($entity->isRequestForRelatedEntity('get_foo_entity', array()), 'foo_entity');
		
		$this->assertFalse($entity->isRequestForRelatedEntity('get_foo_!entity', array()));
		$this->assertFalse($entity->isRequestForRelatedEntity('poo', array()));
		$this->assertFalse($entity->isRequestForRelatedEntity('poo_get_foo', array()));
	}
	
	public function testGetForeignKey()	{
		$entity = new Entity();
		
		$primaryKey = array('col1', 'col2');
		$foreignKey = array ('Entity_col1', 'Entity_col2');
		
		$entity->setPrimaryKey($primaryKey);
		$this->assertEqual($entity->getForeignKeyNames(), $foreignKey);
	}

	/**
	 * Test comparing the primary key column names against a
	 * predefined list
	 */
	public function testCheckingPrimaryKey()	{
		$e = new Entity();
		$e->setPrimaryKey(array('col1', 'col2'));
		
		$this->assertTrue($e->doesPrimaryKeyMatch(array('col2' => 1, 'col1' => 1)));
		$this->assertTrue($e->doesPrimaryKeyMatch(array('col1' => 1, 'col2' => 1)));
		$this->assertFalse($e->doesPrimaryKeyMatch(array('col1' => 1)));
		$this->assertFalse($e->doesPrimaryKeyMatch(array('col999' => 1, 'col777' => 1)));
	}
}
?>